import routes from './routes'
import { Suspense } from 'react'
import { Route,Switch,Redirect } from 'react-router-dom'
import Loading from '../components/Loading/Loading'
interface IProps {
    routes?: Array<any>;
    RedirectPath?: string
}
const MyRouter = (props: IProps) => {
    return (
        <Suspense fallback={(<Loading />)}>
            <Switch>
                {
                    // 如果传入了路由词典，则渲染传入的路由，没有则渲染主路由
                    (props.routes ? props.routes : routes).map((item,index) => {
                        return (
                            <Route key={index}
                                        path={item.path}
                                        render={() => {
                                            return (
                                                <item.component />
                                            )
                                        }} 
                            />
                        )
                    })
                }
                {/* 如果传入了重定向值则渲染，如果没有则不进行渲染 */}
                { props.RedirectPath? (
                    <Redirect  to={props.RedirectPath}/>
                ) : (<></>)}
            </Switch>
        </Suspense>
    )
}

export default MyRouter